/**
 * Copyright (c) 2010 CWI Amsterdam and others. All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     CWI Amsterdam - initial API and implementation
 *
 * $Id$
 */
package nl.cwi.arne.mcrl2;

import java.util.Collection;

import nl.cwi.arne.mcrl2.factory.MCRL2Package;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * @see cwi.reo.mcrl2.MCRL2Package#getElement()
 * @model kind="class"
 * @generated
 */
public class Element extends Nameable {

	/**
	 * @generated NOT
	 */
	public Element(String name, Atom... parameters) {
		super();
		setName(name);
		for (Atom param : parameters) {
			getParameters().add(param);
		}
	}
	
	/**
	 * @generated NOT
	 */
	@Override
	public String toString() {
		StringBuffer result = new StringBuffer(getName());
		if (!getParameters().isEmpty()) {
			result.append("(");
			for (int i=0; i<getParameters().size(); i++) {
				result.append(getParameters().get(i));
				if (i<getParameters().size()-1) result.append(", ");
			}
			result.append(")");
		}
		if (getDiscriminator()!=null) {
			result.append("?" + getDiscriminator().getName());
		}
		return result.toString();
	}
	
	
	/* ---------------------------------------------------------------- *
	 * GENERATED CODE.                                                  *
	 * Do not edit below this line. If you need to edit, move it above  *
	 * this line and change the '@generated'-tag to '@generated NOT'.   *
	 * ---------------------------------------------------------------- */
	
	/**
	 * The cached value of the '{@link #getParameters() <em>Parameters</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getParameters()
	 * @generated
	 * @ordered
	 */
	protected EList<Atom> parameters;
	
	/**
	 * The cached value of the '{@link #getDiscriminator() <em>Discriminator</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDiscriminator()
	 * @generated
	 * @ordered
	 */
	protected Function discriminator;

	/**
	 * @generated
	 */
	public Element() {
		super();
	}

	/**
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return MCRL2Package.Literals.ELEMENT;
	}

	/**
	 * Returns the value of the '<em><b>Declarations</b></em>' containment reference list.
	 * The list contents are of type {@link cwi.reo.mcrl2.Declaration}.
	 * @return the value of the '<em>Declarations</em>' containment reference list.
	 * @see cwi.reo.mcrl2.MCRL2Package#getElement_Declarations()
	 * @model containment="true"
	 * @generated
	 */
	public EList<Atom> getParameters() {
		if (parameters == null) {
			parameters = new EObjectContainmentEList<Atom>(Atom.class, this, MCRL2Package.ELEMENT__PARAMETERS);
		}
		return parameters;
	}

	/**
	 * Returns the value of the '<em><b>Structure</b></em>' container reference.
	 * It is bidirectional and its opposite is '{@link nl.cwi.arne.mcrl2.Structure#getElements <em>Elements</em>}'.
	 * <!-- begin-user-doc -->
	 * <p>
	 * If the meaning of the '<em>Structure</em>' container reference isn't clear,
	 * there really should be more of a description here...
	 * </p>
	 * <!-- end-user-doc -->
	 * @return the value of the '<em>Structure</em>' container reference.
	 * @see #setStructure(Structure)
	 * @see nl.cwi.arne.mcrl2.factory.MCRL2Package#getElement_Structure()
	 * @see nl.cwi.arne.mcrl2.Structure#getElements
	 * @model opposite="elements" transient="false"
	 * @generated
	 */
	public Structure getStructure() {
		if (eContainerFeatureID() != MCRL2Package.ELEMENT__STRUCTURE) return null;
		return (Structure)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetStructure(Structure newStructure, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newStructure, MCRL2Package.ELEMENT__STRUCTURE, msgs);
		return msgs;
	}

	/**
	 * Sets the value of the '{@link nl.cwi.arne.mcrl2.Element#getStructure <em>Structure</em>}' container reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @param value the new value of the '<em>Structure</em>' container reference.
	 * @see #getStructure()
	 * @generated
	 */
	public void setStructure(Structure newStructure) {
		if (newStructure != eInternalContainer() || (eContainerFeatureID() != MCRL2Package.ELEMENT__STRUCTURE && newStructure != null)) {
			if (EcoreUtil.isAncestor(this, newStructure))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newStructure != null)
				msgs = ((InternalEObject)newStructure).eInverseAdd(this, MCRL2Package.STRUCTURE__ELEMENTS, Structure.class, msgs);
			msgs = basicSetStructure(newStructure, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, MCRL2Package.ELEMENT__STRUCTURE, newStructure, newStructure));
	}

	/**
	 * Returns the value of the '<em><b>Discriminator</b></em>' containment reference.
	 * <!-- begin-user-doc -->
	 * <p>
	 * If the meaning of the '<em>Discriminator</em>' containment reference isn't clear,
	 * there really should be more of a description here...
	 * </p>
	 * <!-- end-user-doc -->
	 * @return the value of the '<em>Discriminator</em>' containment reference.
	 * @see #setDiscriminator(Function)
	 * @see nl.cwi.arne.mcrl2.factory.MCRL2Package#getElement_Discriminator()
	 * @model containment="true"
	 * @generated
	 */
	public Function getDiscriminator() {
		return discriminator;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetDiscriminator(Function newDiscriminator, NotificationChain msgs) {
		Function oldDiscriminator = discriminator;
		discriminator = newDiscriminator;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, MCRL2Package.ELEMENT__DISCRIMINATOR, oldDiscriminator, newDiscriminator);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * Sets the value of the '{@link nl.cwi.arne.mcrl2.Element#getDiscriminator <em>Discriminator</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @param value the new value of the '<em>Discriminator</em>' containment reference.
	 * @see #getDiscriminator()
	 * @generated
	 */
	public void setDiscriminator(Function newDiscriminator) {
		if (newDiscriminator != discriminator) {
			NotificationChain msgs = null;
			if (discriminator != null)
				msgs = ((InternalEObject)discriminator).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - MCRL2Package.ELEMENT__DISCRIMINATOR, null, msgs);
			if (newDiscriminator != null)
				msgs = ((InternalEObject)newDiscriminator).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - MCRL2Package.ELEMENT__DISCRIMINATOR, null, msgs);
			msgs = basicSetDiscriminator(newDiscriminator, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, MCRL2Package.ELEMENT__DISCRIMINATOR, newDiscriminator, newDiscriminator));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case MCRL2Package.ELEMENT__STRUCTURE:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetStructure((Structure)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case MCRL2Package.ELEMENT__PARAMETERS:
				return ((InternalEList<?>)getParameters()).basicRemove(otherEnd, msgs);
			case MCRL2Package.ELEMENT__STRUCTURE:
				return basicSetStructure(null, msgs);
			case MCRL2Package.ELEMENT__DISCRIMINATOR:
				return basicSetDiscriminator(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID()) {
			case MCRL2Package.ELEMENT__STRUCTURE:
				return eInternalContainer().eInverseRemove(this, MCRL2Package.STRUCTURE__ELEMENTS, Structure.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case MCRL2Package.ELEMENT__PARAMETERS:
				return getParameters();
			case MCRL2Package.ELEMENT__STRUCTURE:
				return getStructure();
			case MCRL2Package.ELEMENT__DISCRIMINATOR:
				return getDiscriminator();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case MCRL2Package.ELEMENT__PARAMETERS:
				getParameters().clear();
				getParameters().addAll((Collection<? extends Atom>)newValue);
				return;
			case MCRL2Package.ELEMENT__STRUCTURE:
				setStructure((Structure)newValue);
				return;
			case MCRL2Package.ELEMENT__DISCRIMINATOR:
				setDiscriminator((Function)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case MCRL2Package.ELEMENT__PARAMETERS:
				getParameters().clear();
				return;
			case MCRL2Package.ELEMENT__STRUCTURE:
				setStructure((Structure)null);
				return;
			case MCRL2Package.ELEMENT__DISCRIMINATOR:
				setDiscriminator((Function)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case MCRL2Package.ELEMENT__PARAMETERS:
				return parameters != null && !parameters.isEmpty();
			case MCRL2Package.ELEMENT__STRUCTURE:
				return getStructure() != null;
			case MCRL2Package.ELEMENT__DISCRIMINATOR:
				return discriminator != null;
		}
		return super.eIsSet(featureID);
	}

}
